package com.jlkf.controller;

import java.sql.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.TblProfessionSortMapper;
import com.jlkf.pojo.TblGuidePage;
import com.jlkf.pojo.TblProfessionSort;
import com.jlkf.service.AdvertService;
import com.jlkf.service.BackCourseInfoService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Controller
@RequestMapping("advert")
public class AdvertController {
	@Autowired
	private AdvertService advertService;
	@Autowired
	private BackCourseInfoService backCourseInfoService;
	@Autowired
	private TblProfessionSortMapper sortMapper;
	
	/**
	 * 获取启动页
	 * @return
	 */
	@RequestMapping("getBootPage")
	@ResponseBody
	public ModelAndView getBootPage(){
		TblGuidePage page = advertService.getBootPage();
		ModelAndView andView = new ModelAndView();
    	andView.addObject("page",page);
    	andView.setViewName("jsp/advertManager/bootPage");
    	return andView;
	}
	
	/**
	 * 获取更多按钮图
	 */
	@RequestMapping("/getMore")
	@ResponseBody
	public ModelAndView getMore(){
		Map<String,Object> result = advertService.getMore();
		ModelAndView andView = new ModelAndView();
		andView.addObject("result",result);
		andView.setViewName("jsp/advertManager/morePage");
		return andView;
	}
	
	/**
	 * 编辑更多按钮图
	 */
	@RequestMapping("/updateMore")
	@ResponseBody
	public ReturnData updateMore(@RequestBody String json){
		TblGuidePage page = StringTools.json2Obj(json, TblGuidePage.class);
		page.setgUpdatetime(new Date());
		int ret = advertService.update(page);
		if(ret>0){
			return new ReturnData(200,"修改成功");
		}else{
			return new ReturnData(500,"修改失败");
		}
	}
	/**
	 * 新增或编辑
	 * @param json
	 * @return
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public ReturnData addOrUpdate(@RequestBody String json){
		try{
			TblGuidePage page = StringTools.json2Obj(json, TblGuidePage.class);
			if(page.getgId()!=null){
				page.setgUpdatetime(new Date());
				int ret = advertService.update(page);
				if(ret>0){
					return new ReturnData(200,"修改成功");
				}else{
					return new ReturnData(500,"修改失败");
				}
			}else{
				page.setgCreatetime(new Date());
				page.setgUpdatetime(new Date());
				if(page.getgType()==5 || page.getgType()==6 || page.getgType()==7){
					Map<String, Object> map = new HashMap<String,Object>();
					map.put("gType", page.getgType());
					map.put("gVersion", page.getgVersion());
					List<TblGuidePage> list = advertService.getGuidPageList(map);
					if(list!=null&&list.size()>0){
						return new ReturnData(500,"已存在该图片，请编辑");
					}
				}
				int ret = advertService.insert(page);
				if(ret>0){
					return new ReturnData(200,"添加成功");
				}else{
					return new ReturnData(500,"添加失败");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"参数传入有误");
		}
	}
	
	/**
	 * 按条件查询列表
	 * @param type
	 * @param startTime
	 * @param endTime
	 * @param title
	 * @param status
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getPageList")
	@ResponseBody
	public ReturnData getPageList(Integer type,String startTime,String endTime,String title,Integer status,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("gType", type);
		if(!StringTools.isEmpty(startTime)){
			map.put("startTime", startTime);
		}
		if(!StringTools.isEmpty(endTime)){
			map.put("endTime", endTime);
		}
		if(!StringTools.isEmpty(title)){
			map.put("title", title);
		}
		if(status!=null){
			map.put("status", status);
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblGuidePage> list = advertService.getGuidPageList(map);
		PageInfo<TblGuidePage> pageInfo = new PageInfo<TblGuidePage>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"轮播或引导页列表");
	}
	
	/**
	 * 批量删除
	 * @param gId
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public ReturnData delete(String gId){
		List<String> list = Arrays.asList(gId.split(","));
		int rel = advertService.delete(list);
		if(rel>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
	
	/**
	 * 获取详情(修改)
	 * @param gId
	 * @return
	 */
	@RequestMapping("getDetail")
	@ResponseBody
	public ModelAndView getDetail(Long gId){
		TblGuidePage page = advertService.getDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("page",page);
    	List<Map<String, Object>> data = backCourseInfoService.selectCourseInfoList(new HashMap<>());
    	for(Map<String, Object> map:data){
    		String G_ID = map.get("G_ID").toString();
    		map.put("G_ID", G_ID);
    	}
    	Map<String, Object> map = new HashMap<String,Object>();
    	map.put("type", 4);
    	List<TblProfessionSort> data1 = sortMapper.selectByType(map);
    	for(TblProfessionSort sort :data1){
    		String g_Id = sort.getgId().toString();
    		sort.setId(g_Id);
    	}
    	map = new HashMap<String,Object>();
    	map.put("type", 3);
    	List<TblProfessionSort> data2 = sortMapper.selectByType(map);
    	for(TblProfessionSort sort :data2){
    		String g_Id = sort.getgId().toString();
    		sort.setId(g_Id);
    	}
    	map = new HashMap<String,Object>();
    	map.put("type", 2);
    	List<TblProfessionSort> data3 = sortMapper.selectByType(map);
    	for(TblProfessionSort sort :data3){
    		String g_Id = sort.getgId().toString();
    		sort.setId(g_Id);
    	}
    	andView.addObject("data", data);
    	andView.addObject("data1", data1);
    	andView.addObject("data2", data2);
    	andView.addObject("data3", data3);
    	if(page.getgType()==3){
    		andView.setViewName("jsp/advertManager/addOrUpdateSlideshow");
    	}else if(page.getgType()==4){
    		andView.setViewName("jsp/advertManager/addOrUpdateAdv");
    	}else if(page.getgType()==5 || page.getgType()==6 || page.getgType()==7 || page.getgType()==8 || page.getgType()==9 || page.getgType()==10){
    		andView.setViewName("jsp/advertManager/addOrUpdateOther");
    	}else if(page.getgType()==1){
    		andView.setViewName("jsp/advertManager/addOrUpdateguidePage");
    	}
    	return andView;
	}
	
	/**
	 * 获取详情(新增)
	 * @param gId
	 * @return
	 */
	@RequestMapping("getDetailByInsert")
	@ResponseBody
	public ModelAndView getDetail(Integer type){
		ModelAndView andView = new ModelAndView();
    	List<Map<String, Object>> data = backCourseInfoService.selectCourseInfoList(new HashMap<>()); //单个课程
    	for(Map<String, Object> map:data){
    		String G_ID = map.get("G_ID").toString();
    		map.put("G_ID", G_ID);
    	}
    	Map<String, Object> map = new HashMap<String,Object>();
    	map.put("type", 4);
    	List<TblProfessionSort> data1 = sortMapper.selectByType(map);
    	map.put("type", 3);
    	List<TblProfessionSort> data2 = sortMapper.selectByType(map);
    	map.put("type", 2);
    	List<TblProfessionSort> data3 = sortMapper.selectByType(map);
    	andView.addObject("data", data);
    	andView.addObject("data1", data1);
    	andView.addObject("data2", data2);
    	andView.addObject("data3", data3);
    	if(type==1){
    		andView.setViewName("jsp/advertManager/addOrUpdateSlideshow");
    	}else if(type==2){
    		andView.setViewName("jsp/advertManager/addOrUpdateAdv");
    	}else if(type==3){
    		andView.setViewName("jsp/advertManager/addOrUpdateOther");
    	}else if(type==4){
    		andView.setViewName("jsp/advertManager/addOrUpdateguidePage");
    	}
    	return andView;
	}
	
	/**
	 * 其他广告图列表
	 */
	@RequestMapping("getOtherList")
	@ResponseBody
	public ReturnData getOtherList(Integer pageNumber,Integer pageSize){
		PageHelper.startPage(pageNumber, pageSize);
		List<TblGuidePage> list = advertService.getOtherList();
		PageInfo<TblGuidePage> pageInfo = new PageInfo<TblGuidePage>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"其他广告图列表");
	}
}
